﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace JuegoBomberman.Menu
{
    public abstract class MenuManager : IDibujable
    {
        protected Color colorElegido;
        protected Color colorNormal;
        protected int itemElegido;
        protected KeyboardState keyboardState;
        protected KeyboardState oldKeyboardState;
        protected SpriteBatch spriteBatch;
        protected ContentManager content;
        protected List<MenuItem> itemsMenu;
        protected JuegoBomberman game;

        public MenuManager(JuegoBomberman game)
        {
            itemsMenu = new List<MenuItem>();
            this.game = game;
        }

        public virtual void LoadContent()
        {
            this.content = game.Content;
            this.spriteBatch = new SpriteBatch(game.GraphicsDevice);
        }

        public virtual void Update(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState();
            itemsMenu.ElementAt(itemElegido).desElegir();

            if (keyboardState.IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyUp(Keys.Down))
            {
                itemElegido++;
                if (itemElegido == itemsMenu.Count())
                {
                    itemElegido = 0;
                }
            }
            else if (keyboardState.IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyUp(Keys.Up))
            {
                itemElegido--;
                if (itemElegido == -1)
                {
                    itemElegido = itemsMenu.Count() - 1;
                }
            }
            itemsMenu.ElementAt(itemElegido).elegir();

            if (keyboardState.IsKeyDown(Keys.Enter) && oldKeyboardState.IsKeyUp(Keys.Enter))
            {
                this.CambiarPantalla();
            }

            oldKeyboardState = keyboardState;
        }

        public virtual void Draw(GameTime gameTime)
        {
            foreach (MenuItem item in itemsMenu)
            {
                item.Draw(spriteBatch);
            }
        }

        public virtual void CambiarPantalla()
        {
        }
    }
}
